{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台-商品分类列表</title>
{% endblock %}

{% block con %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">商品分类列表</div>
                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <a href="{% url 'myadmin_cates_add' %}" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</a>
                                </div>
                            </div>
                        </div>
                    </div>
                <form>
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select name='types' data-am-selected="{btnSize: 'sm'}" style="display: none;">
                              <option value="title">商品分类名称</option>
                            </select>
                        </div>
                    </div>

                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input value="{{ request.GET.keywords }}" name="keywords" type="text" class="am-form-field ">
                            <span class="am-input-group-btn">
                                <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search" ></button>
                              </span>
                        </div>
                    </div>
                </form>
                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>分类名</th>
                                    <th>所属分类名</th>
                                    <th>path</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for v in cateslist %}
                                <tr>
                                    <td>{{ v.id }}</td>
                                    {% load pagetag %}
                                    <td class="am-text-middle editname">
                                        <span>{% sj v.path %}</span>
                                        <span class="name">{{ v.name }}</span>
                                    </td>
                                    <td class="am-text-middle">{{ v.pid }}</td>
                                    <td class="am-text-middle">{{ v.path }}</td>
                                    <td class="am-text-middle">
                                        {% if v.isDelete %}
                                            删除
                                        {% else %}
                                            正常
                                        {% endif %}
                                    </td>
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">
                                            <a href="javascript:void(0)" class="tpl-table-black-operation-edit">
                                                <i class="am-icon-pencil"></i> 编辑
                                            </a>
                                            <a href="javascript:void(0)" class="tpl-table-black-operation-del">
                                                <i class="am-icon-trash"></i> 删除
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                      
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">

                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                                {% load pagetag %}

                                {% ShowPages cateslist.paginator.num_pages request %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    // 给编辑按钮绑定单击事件
    $('.tpl-table-black-operation-edit').click(function(){
        // 获取当前所选的分类id
        var cid = $(this).parents('tr').find('td:eq(0)').text()
        var name = $(this).parents('tr').find('.name').text()
        // 动态创建input,放到name的位置
        var inp = $('<input value="' + name + '" style="color:black;">')
        $(this).parents('tr').find('.name').html(inp)
        inp.select()
        var t = $(this).parents('tr').find('.editname')

        // 给input绑定丧失焦点事件
        inp.blur(function () {
            // 获取当前input中修改的值
            var xname = $(this).val()
            // 判断新的值和原来的值是否一致
            if (xname == name) {
                // 恢复原样
                t.find('.name').html(name)
            } else {
                // 发送ajax请求去修改
                // console.log(xname,cid)
                $.get('{% url "myadmin_cates_edit" %}', { 'cid': cid, 'name': xname }, function (data) {
                    // 恢复原样
                    t.find('.name').html(xname)
                    alert(data['msg'])
                }, 'json')
            }
        })
    })
    // 给td绑定双击事件,进行修改
    $('tbody .editname').dblclick(function(){
        // 获取当前的分类名
        var name = $(this).find('.name').text()
        var cid = $(this).prev().text()
        // 动态创建input,放到name的位置
        var inp = $('<input value="'+name+'" style="color:black;">')
        $(this).find('.name').html(inp)
        inp.select()
        var t = $(this)

        // 给input绑定丧失焦点事件
        inp.blur(function(){
            // 获取当前input中修改的值
            var xname = $(this).val()
            // 判断新的值和原来的值是否一致
            if(xname == name){
                // 恢复原样
                t.find('.name').html(name)
            }else{
                // 发送ajax请求去修改
                // console.log(xname,cid)
                $.get('{% url "myadmin_cates_edit" %}',{'cid':cid,'name':xname},function(data){
                    // 恢复原样
                    t.find('.name').html(xname)
                    alert(data['msg'])
                },'json')
            }
        })
    })
    // 给删除按钮绑定单击事件
    $('.tpl-table-black-operation-del').click(function(){
        // 获取当前所选的分类id
        var cid = $(this).parents('tr').find('td:eq(0)').text()
        var a = $(this)
        // 发送ajax请求
        $.get('{% url "myadmin_cates_del" %}',{'cid':cid},function(data){
            // 判断是否已经删除成功
            if(data['error'] == 0){
                // 删除成功,动态的删除当前这一条分类数据
                // $(this).parents('tr').remove() // X
                // console.log($(this)) // XMLHttpRequest ajax对象
                a.parents('tr').remove()
            }
            alert(data['msg'])
        },'json')
    })
</script>
{% endblock %}